﻿@using System.Activities.DurableInstancing
@using System.Data
@{
    Layout = "~/Areas/Admin/Views/Shared/AdminLayout.cshtml";
}
<script src="/Scripts/jquery-1.8.2.min.js"></script>
<script src="/Scripts/ajaxfileupload.js"></script>
<script src="/Scripts/kindeditor/kindeditor-all-min.js"></script>
<link href="/Scripts/kindeditor/themes/default/default.css" rel="stylesheet" />
<link rel="stylesheet" href="/Scripts/kindeditor/plugins/code/prettify.css" />
<script charset="utf-8" src="/Scripts/kindeditor/lang/zh-CN.js"></script>
<script charset="utf-8" src="/Scripts/kindeditor/plugins/code/prettify.js"></script>
<script src="~/Scripts/kindeditor/plugins/insertfile/insertfile.js"></script>
<style>
   
    </style>

<input name="nimg" id="nimg" type="hidden" value="" />
<input name="tdes" id="tdes" type="hidden" value="" />
<input name="newscontent" id="newscontent" type="hidden" />
<div style="width:100%;height:500px; overflow:scroll; border:1px solid;">
   
    <table style="margin-left: 10px; font-size: 12px;">
        <tr>
            <td><label>文章标题</label></td>
            <td><input type="text" id="title" style="width: 300px;" /></td>
        </tr>
        <tr>
            <td><label>原文链接</label></td>
            <td><input type="text" id="url" style="width: 400px;" /></td>
        </tr>

        <tr>
            <td><label>原文描述</label></td>
            <td>
                <textarea rows="7" cols="50" id="des"></textarea>
            </td>
        </tr>
        <tr>
            <td>
                <a style="width: 70px;height: 30px;display: block; background: #666;color: #fff;line-height: 30px;text-align: center;" onclick="checkfile()">上传图片</a>
                <input id="fileToUpload" style="display: none; cursor: pointer;" type="file" onchange="upload()" name="upfile">
            </td>
            <td>
                <img src="" id="newimg" style=" width: 250px; height: 150px;" />
            </td>
        </tr>
        <tr>
            <td rowspan="2"><label>文章内容</label></td>

        </tr>
        <tr>
            <td rowspan="2">
                <textarea id="content1" style="width:600px;height:400px;visibility:hidden;"></textarea>
            </td>

        </tr>

        <tr>
            <td rowspan="2"></td>
        </tr>
    </table>
    <div style="width: 1000px; height: 40px; margin-top: 50px;">
        <div class="botton" style="margin-left: 340px;" onclick="edit()">保存</div>
        <div class="botton" style="margin-left: 50px;" onclick="history.go(-1)">取消</div>
    </div>

</div>
  

<script>
    var editor;
    KindEditor.ready(function (K) {

        editor = K.create('#content1', {
            cssPath: '/Scripts/kindeditor/plugins/code/prettify.css',
            uploadJson: '/Scripts/kindeditor/aspnet/upload_json.ashx',
            fileManagerJson: '/Scripts/kindeditor/aspnet/file_manager_json.ashx',
            allowFileManager: true,
            afterCreate: function () {
                var self = this;
                K.ctrl(document, 13, function () {
                    self.sync();
                    K('form[name=example]')[0].submit();
                });
                K.ctrl(self.edit.doc, 13, function () {
                    self.sync();
                    K('form[name=example]')[0].submit();
                });
            }, afterUpload: function (url) {
                var arr = url.split('<');
                if (arr.length == 2) {
                    $("#keUrl").val(arr[0]);
                    $("#keTitle").val(arr[1]);
                }
            }
        });
        prettyPrint();
    });
        var id = "@ViewData["id"]";

    if (id != "0") {
        $.post("@Url.Action("GetOneData")", { id: id }, function(data) {
                var obj = JSON.parse(data);
                if (obj.length > 0) {
                    editor.html(obj[0].Content);
                    $("#title").val(obj[0].Title);
                    $("#newimg").attr("src", obj[0].Img);
                    $("#nimg").val(obj[0].Img);
                    $("#url").val(obj[0].Url);
                    $("#des").val(obj[0].Des);
                } 

            });
        }
      
        function edit() {

            var title = $("#title").val();
            var text = editor.html(); //描述
            var img = $("#nimg").val();
            var des = $("#des").val();
            var url = $("#url").val();
            if (url.replace(/\s/g, "").length == 0) {
                alert("请输入原文链接！");
                return false;
            }
            if (des.replace(/\s/g, "").length == 0) {
                alert("请输入文章描述！");
                return false;
            }
            if (title.replace(/\s/g, "").length == 0) {
                alert("请输入文章标题！");
                return false;
            }
            if (text.replace(/\s/g, "").length == 0) {
                alert("请输入文章内容！");
                return false;
            }
            if (img.replace(/\s/g, "").length == 0) {
                alert("请上传图片！");
                return false;
            }
            $.post("@Url.Action("EditArticle")", { id: id, title: title, content: encodeURIComponent(text), img: img, des: des,url:url}, function (row) {
                if (row > 0) {
                    alert("操作成功！");
                } else {

                    alert("操作失败！");
                }

                location.href = "@Url.Action("Index")";
            });
        }

        function checkfile() {
            $('#fileToUpload').click();
        }

        function upload() {
            var val = $("#fileToUpload").val();
            if (val == "") {
                alert("请上传文件");
                return false;
            }
            var suffix = val.split('.');
            var arr = new Array("png", "jpg", "gif");
            if (arr.indexOf(suffix[suffix.length - 1]) < 0) {
                alert("上传格式有误！");
            } else {

                $.ajaxFileUpload({
                    url: '@Url.Action("UploadImg")',
                    secureuri: false,
                    fileElementId: 'fileToUpload',
                    dataType: 'json',
                    success: function(data, status) {
                        var str = "";
                        if (data.status == 1) {
                            $("#newimg").attr("src", data.path);
                            $("#nimg").val(data.path);
                        } else {

                        }

                    }
                });
            }
        }


</script>
